package com.zhouchao.web;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhouchao.bean.Goods;
import com.zhouchao.dao.GoodsDao;
import com.zhouchao.util.DbUtil;
import com.zhouchao.util.TimeUtil;
/**
 * 物品登记
 * 
 * @author zhouchao
 *
 */
public class Goodsservlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		TimeUtil time = new TimeUtil();
		String tm = time.Time();
		DbUtil dbUtil = new DbUtil();
		String action = request.getParameter("action");
		Connection con = null;
		GoodsDao gd=new GoodsDao();
		if("into".equals(action)){
			System.out.println("1111111111111111111");
			request.setAttribute("time", tm);
			request.setAttribute("mainPage", "dormManager/goods.jsp");
			request.getRequestDispatcher("mainManager.jsp").forward(request, response);
			
			
			
		}else if("all".equals(action)){
			try {
				con=dbUtil.getCon();
				List<Goods> goodslist=new ArrayList<Goods>();
				goodslist=gd.infoList(con);
				request.setAttribute("goodslist", goodslist);
				request.setAttribute("mainPage", "dormManager/goodslist.jsp");
				request.getRequestDispatcher("mainManager.jsp").forward(request, response);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}else{
			System.out.println("22222222222222222222222222");
			String buildid = request.getParameter("buildid");
			String dorm = request.getParameter("dorm");
			String name=request.getParameter("name");
			//String name=new String(request.getParameter("name").getBytes("utf-8"),"utf-8");
			//System.out.println("name1"+name1+"name"+name);
			
			String num = request.getParameter("num");
			String goods=request.getParameter("goods");
			//String goods=new String(request.getParameter("goods").getBytes("ISO-8859-1"),"utf-8");
			String date = request.getParameter("date");
			String tt=request.getParameter("tt");
			//String tt=new String(request.getParameter("tt").getBytes("ISO-8859-1"),"utf-8");
			Goods gs=new Goods(buildid,dorm,name,num,goods,date,tt);
			System.out.println(gs);
			
			try {
				con=dbUtil.getCon();
				gd.intoInfo(con, gs);
				request.setAttribute("time", tm);
				request.setAttribute("mainPage", "dormManager/goods.jsp");
				request.getRequestDispatcher("mainManager.jsp").forward(request, response);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
	}

}
